package com.tianji.aigc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.aigc.config.SessionProperties;
import com.tianji.aigc.entity.ChatSession;
import com.tianji.aigc.mapper.ChatSessionMapper;
import com.tianji.aigc.service.IChatSessionService;
import com.tianji.aigc.vo.SessionVO;
import com.tianji.common.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 对话session 服务实现类
 * </p>
 *
 * @author z
 * @since 2025-09-02
 */
@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession> implements IChatSessionService {

    @Autowired
    private SessionProperties sessionProperties;

    /**
     * 创建新会话
     *
     * @param num
     * @return
     */
    @Override
    public SessionVO createChat(Integer num) {
        //生成会话id
        String sessionId = IdUtil.fastSimpleUUID();
        //保存会话历史
        ChatSession chatSession = ChatSession.builder().sessionId(sessionId).userId(UserContext.getUser()).build();
        this.save(chatSession);
        //随机取出3个热门问题
        List<SessionVO.Example> examples = sessionProperties.getExamples();
        Collections.shuffle(examples);//打乱问题
        //封装结果
        SessionVO sessionVO = SessionVO.builder()
                .sessionId(sessionId)
                .title(sessionProperties.getTitle())
                .describe(sessionProperties.getDescribe())
                .examples(examples.stream().limit(num).toList())
                .build();
        return sessionVO;
    }

    /**
     * 更换热门内容
     * @param num
     * @return
     */
    @Override
    public List<SessionVO.Example> changeChat(Integer num) {
        //随机取出3个热门问题
        List<SessionVO.Example> examples = sessionProperties.getExamples();
        Collections.shuffle(examples);//打乱问题
        //封装结果
        return examples;
    }
}
